/**
 * 
 */
package com.mobilottery.server;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Provides functions to store and fetch games
 * @author ziv
 */
public class GameManager {

    public void createNewGame(Game game) 
    {
            PersistenceManager.getInstance().persist(game);
    }


    /**
     * Returns games which closed before current time
     * @return
     */
    public List<Game> getActiveGames() {
        //create an sql string
        String hql = "select g from Game as g where g.closeDate > :now and g.openDate < :now  and  g.winningNumbersRaw IS NULL order by g.id desc";
        //create parameters
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("now", new Date());
        return PersistenceManager.getInstance().selectParamQuery(hql, parameters);
    }
    
    /**
     * Returns games which closed before current time
     * @return
     */
    public List<Game> getFinishedGames() {
        //create an sql string
        String hql = "select g from Game as g where g.winningNumbersRaw IS NOT NULL  order by g.id desc";
        return PersistenceManager.getInstance().selectQuery(hql);
    }    
    
    /**
     * Returns games which closed before current time
     * @return
     */
    public List<Game> getAllGames() 
    {
        String hql = "select g from Game as g order by g.id desc";
        return PersistenceManager.getInstance().selectQuery(hql);
    }    
    

    public List<GameParticipation> getUserParticipations(Long userId) {
        String hql = "select g from GameParticipation as g where g.user.id = :gameUserId  order by g.id desc";
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("gameUserId", userId);
        return PersistenceManager.getInstance().selectParamQuery(hql, parameters);
    }
    
    public List<GameParticipation> getUserActiveParticipations(Long userId) {
        String hql = "select g from GameParticipation as g where g.user.id = :gameUserId and g.winningAmount IS NULL order by g.id desc";
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("gameUserId", userId);
        return PersistenceManager.getInstance().selectParamQuery(hql, parameters);
    }    
    
    public List<GameParticipation> getUserPlayedParticipations(Long userId) {
        String hql = "select g from GameParticipation as g where g.user.id = :gameUserId and g.winningAmount IS NOT NULL  order by g.id desc";
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("gameUserId", userId);
        return PersistenceManager.getInstance().selectParamQuery(hql, parameters);
    }    
    
    public List<GameParticipation> getGameParticipations(Long gameId) {
        String hql = "select g from GameParticipation as g where g.game.id = :gameId order by g.id desc";
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("gameId", gameId);
        return PersistenceManager.getInstance().selectParamQuery(hql, parameters);
    }    

    public Game getGame(Long id) {
        return PersistenceManager.getInstance().getByKey(Game.class, id);
    }
    
    public Game getGameByExternalId(Long externalId) {
        String hql = "select g from Game as g where g.externalId = :externalId";
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("externalId", externalId);
        return PersistenceManager.getInstance().selectParamSingleQuery(hql, parameters);
    }    
}
